package com.itheima.thread.Demo5;

import java.util.concurrent.locks.ReentrantLock;

public class LockTicket implements Runnable {
    private int ticket = 100;
    private ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while(true){
            while (true) {
                try {
                    lock.lock();
                    if (ticket == 0) {
                        break;
                    } else {
                        Thread.sleep(10);
                        ticket--;
                        System.out.println(Thread.currentThread().getName() + "正在买票,剩余" + ticket + "张");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }
}
